﻿using System;
using System.ComponentModel;
using System.Runtime.Remoting.Channels;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Heyo.Class.Animations;

namespace Heyo.Controls.Animations
{
    public sealed class MatrixAnimationBorder:AnimationBorder
    {
        public override Duration Duration { get; set; } = Duration.Automatic;
        public override double SpeedRatio { get; set; } = 1;
        public static readonly DependencyProperty TransFromProperty = DependencyProperty.Register(
            "TransFrom", typeof(Transform), typeof(MatrixAnimationBorder), new PropertyMetadata(new TranslateTransform()));

        [Category("Animation")]
        public Transform TransFrom
        {
            get => (Transform) GetValue(TransFromProperty);
            set => SetValue(TransFromProperty, value);
        }

        public static readonly DependencyProperty TransToProperty = DependencyProperty.Register(
            "TransTo", typeof(Transform), typeof(MatrixAnimationBorder), new PropertyMetadata(new TranslateTransform()));

        [Category("Animation")]
        public Transform TransTo
        {
            get => (Transform) GetValue(TransToProperty);
            set => SetValue(TransToProperty, value);
        }

        protected override DependencyProperty AnimationProperty => MatrixTransform.MatrixProperty;

        protected override IAnimatable AnimationElement => _transform;

        private readonly MatrixTransform _transform=new MatrixTransform();

        public MatrixAnimationBorder()
        {
            RenderTransform = _transform;
        }
        protected override AnimationTimeline InitAnimation()
        {
            MatrixAnimation anim = new MatrixAnimation { Duration = Duration, SpeedRatio = SpeedRatio, EasingFunction = AnimFunction };

            if (TransFrom != null)
            {
                anim.From = TransFrom.Value;
            }

            if (TransTo != null)
            {
                anim.To = TransTo.Value;
            }

            return anim;
            //_transform.BeginAnimation(MatrixTransform.MatrixProperty, anim);
        }

        protected override AnimationTimeline InitInverseAnimation()
        {
            MatrixAnimation anim = new MatrixAnimation { Duration = Duration, SpeedRatio = SpeedRatio, EasingFunction = AnimFunction };
            if (TransFrom != null)
            {
                anim.To = TransFrom.Value;
            }

            if (TransTo != null)
            {
                anim.From = TransTo.Value;
            }

            return anim;
            //_transform.BeginAnimation(MatrixTransform.MatrixProperty, anim);
        }
    }
}
